package models;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;

import common.annotation.UI_Field;
import common.annotation.UI_Model;
import common.annotation._Compnent;

@Entity
@UI_Model(showName="线路", maxField=7)
public class Trip extends _Model {


	@UI_Field( showName="线路名称", compnent=_Compnent.input  )
	public String tripName ;
	@UI_Field( showName="线路类型", compnent=_Compnent.select, relationModel="tripTypes",relationField="typeName" )
	public String tripType ;
	public String cityCode ;
	public String showImg ;
	@UI_Field( showName="天数", compnent=_Compnent.input )
	public int tripDay ;
	public String tripSchedule ;
	public String costDescription ;
	public String beCareful ;
	public int state ;
	@UI_Field( showName="销售量" )
	public int salesCount ;
	@UI_Field(  showName="浏览量" )
	public int browCount ;

	@UI_Field( showName="集合地", compnent=_Compnent.checkbox, relationModel="tripStartAddresses",relationField="addressName")
	@ManyToMany
	public List<TripStartAddress> tripStartAddress ;
	
	
	/**
	 * 测试设置与tripStartDate进行双向修改
	 * 
	@UI_Field( showName="时间/成价/儿价", compnent=_Compnent.checkbox, relationModel="tripStartDates",relationFields= {"tripIdCalendar","adultPrice","childrenPrice"})
	@ManyToMany
	@JoinTable(name = "Trip_TripStartDate",
			joinColumns={@JoinColumn(name="trip_id")},  
            inverseJoinColumns={@JoinColumn(name="tripStartDates_id")}
	)
	*
	* 注意:双向修改以为两方都要对关联表数据进行维护，
	* 	也就是对于其他信息进行单独修改也必须带入关联维护信息这样会比较码放
	* 	还是建议有其中一方进行关系维护
	*/
	@UI_Field( showName="时间/成价/儿价", compnent=_Compnent.checkbox, relationModel="tripStartDates",relationFields= {"tripIdCalendar","adultPrice","childrenPrice"})
	@ManyToMany
	public List<TripStartDate> tripStartDates ;           
	
	public static void main(String[] args) {
		Set<Field> fields = new LinkedHashSet<Field>();
		Collections.addAll(fields, Trip.class.getDeclaredFields() ) ;
		fields.forEach(field->{
			// 判定此 Class 对象所表示的类或接口与指定的 Class 参数所表示的类或接口是否相同，或是否是其超类或超接口。
			if ( Collection.class.isAssignableFrom(field.getType()) )
			{
				System.err.println(field.getName());
				System.err.println( field.getGenericType());
				// 将list中的泛型对去类型读取出来(ParameterizedType)(getActualTypeArguments-返回表示此类型实际类型参数的 Type对象的数组。)
				final Class<?> fieldType = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
				System.err.println( fieldType.getName() );
			}
		});
		
	    
	}
}
